import os
import shutil
from conans import ConanFile, AutoToolsBuildEnvironment, tools
import sys
sys.path.append(os.environ.get('PYTHONPATH'))
from pathlib import Path


class NASMConan(ConanFile):
    name = "nasm"
    url = "https://github.com/conan-io/conan-center-index"
    homepage = "http://www.nasm.us"
    description = "The Netwide Assembler, NASM, is an 80x86 and x86-64 assembler"
    license = "BSD-2-Clause"
    settings = "os", "arch", "compiler", "build_type"
    topics = ("conan", "nasm", "installer", "assembler")
    exports_sources = "patches/**"
    _autotools = None
    version = "2.15.05"
    urlwin32 = "https://www.nasm.us/pub/nasm/releasebuilds/2.15.04/win32/nasm-2.15.04-win32.zip"

    def source(self):
        print("source  cwd is", os.getcwd())
        print("sys path is", sys.path)
        my_file = Path("nasm-2.15.04-win32.zip")
        print(my_file.exists())
        if my_file.exists() == False:
            tools.download(
                self.urlwin32, "nasm-2.15.04-win32.zip")

    def build_requirements(self):
        if tools.os_info.is_windows and not tools.which("perl"):
            self.build_requires("strawberryperl/5.30.0.1")

    def package_id(self):
        del self.info.settings.compiler

    def build(self):
        tools.unzip("nasm-2.15.04-win32.zip", "")

    def package(self):
        if self.settings.os == "Windows":
            self.copy(pattern="*.exe", src="nasm-2.15.04",
                      dst="bin", keep_path=False)

    def package_info(self):
        bin_path = os.path.join(self.package_folder, "bin")
        self.output.info("Appending PATH environment variable: %s" % bin_path)
        self.env_info.PATH.append(bin_path)
